package com.s10.ecloud.web.model;

// default package

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;

@Entity
@Table(name = "DEVICEMODEL", catalog = "ECLOUD")
public class DeviceModel implements java.io.Serializable {

	private static final long serialVersionUID = 1L;
	private Integer id;
	private String name;
	private DeviceType deviceType;

	private EnterpriseUser user;
	private String description;
	private String state;
	private Set<Log> logs = new HashSet<Log>(0);
	private List<Command> commands = new ArrayList<Command>();
	private String address;

	// Constructors

	/** default constructor */
	public DeviceModel() {
	}

	/** full constructor */
	public DeviceModel(String name, String state, Set<Log> logs,
			EnterpriseUser user) {
		this.name = name;
		this.state = state;
		this.logs = logs;
		this.user = user;
	}

	// Property accessors
	// 这里用了新的id生成策略 uuid.string(注意 如果使用uuid.string 则会出错 hibernate3已经不支持这种生成方式)
	@Id
	@GeneratedValue
	@Column(name = "id", unique = true, nullable = false, length = 32)
	public Integer getId() {
		return this.id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	@Column(name = "name", length = 32)
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@Column(name = "description", length = 32)
	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	// 这里指自身与别的表做外键 所以是ManyToOne
	@ManyToOne(fetch = FetchType.EAGER)
	// 设置自身哪个属性与别的表的主键做外键
	@JoinColumn(name = "typeId")
	public DeviceType getDeviceType() {
		return this.deviceType;
	}

	public void setDeviceType(DeviceType type) {
		this.deviceType = type;
	}

	// 这里指自身与别的表做外键 所以是ManyToOne
	@ManyToOne(fetch = FetchType.EAGER)
	// 设置自身哪个属性与别的表的主键做外键
	@JoinColumn(name = "userid")
	public EnterpriseUser getUser() {
		return this.user;
	}

	public void setUser(EnterpriseUser user) {
		this.user = user;
	}

	@Column(name = "state", length = 1)
	public String getState() {

		return this.state;
	}

	public void setState(String state) {
		this.state = state;
	}

	@Column(name = "address", length = 58)
	public String getAddress() {
		return this.address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "equipment")
	public Set<Log> getLogs() {
		return this.logs;
	}

	public void setLogs(Set<Log> logs) {
		this.logs = logs;
	}

	public void putCommands(List<Command> commands) {
		this.commands = commands;

	}

	public List<Command> receiveCommands() {
		List<Command> commands_return = this.commands;
		// //System.out.println("commands_return"+commands_return);
		// //System.out.println("commands_return"+this.getVendorDevice());
		// //System.out.println(commands_return==null ||
		// commands_return.size()<=0);
		/*
		 * if ((commands_return == null || commands_return.size() <= 0) &&
		 * this.getVendorDevice() != null) { commands_return =
		 * this.getVendorDevice().receiveCommands();
		 */
		// //System.out.println("name:"+this.getVendorDevice().getEquipname());
		// //System.out.println("name:"+this.getVendorDevice().receiveCommands());
		// }
		return commands_return;
	}

}